

#include "thread_open.h"

#include <QTimer>
#include <QTime>
#include <QDebug>
#include <qdatetime.h>

#include <iostream>
#include <fstream>

using namespace std;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>
#include "global.h"

//#include "/home/yu/QTworkspace/VDMA.c"

QTime start_time;
QTime end_time;

QTime start_time0;
QTime end_time0;

int rx_temp=0;
// debug params
short in_802816[802816]={0};
int debug=0;
int readstate_open = 1 - debug;
// end debug

void camera_get(int shownum)
{

    if(USE_CAMERA)ioctl(fd, VIDIOC_DQBUF, &buf);

    // Process the frame
    QTime start_timerd;
    start_timerd = QTime::currentTime();

    //cvSetData(iplImage,framebuf[buf.index].start,VIDEO_WIDTH*2);
    //qDebug() << "buf.index"<< (int)buf.index;
    memcpy(fbaddr0[buf.index].fbV, framebuf[buf.index].start, VIDEO_WIDTH*VIDEO_HEIGHT*2*sizeof(char));
    // copy from framebuf to fbaddr0


    QTime end_timewr;
    end_timewr = QTime::currentTime();

    //qDebug() << "vdma_start_buffering";

    vdma_start_buffering_set(&vdma0, VDMA_WRITE);
    //qDebug() << "vdma_start_0";
    vdma_start_buffering_set(&vdma1, VDMA_READ);
    //qDebug() << "vdma_start_1";
    vdma_start_buffering_set(&vdma2, VDMA_READ);
    //qDebug() << "vdma_start_2";

    msync((framecontrol[0].start), 24*sizeof(int), MS_SYNC);

    //qDebug() << "msync";

    framecontrol[0].RS = (framecontrol[0].start)[0];
    //framecontrol[0].RS = 1;

    framecontrol[0].WK_ID = tx_id;
    (framecontrol[0].start)[5] = framecontrol[0].WK_ID;

    framecontrol[0].lOCK_STATE = (framecontrol[0].start)[3];

    framecontrol[0].RX_ID = (framecontrol[0].start)[6];

    if(framecontrol[0].lOCK_STATE == 0){
        framecontrol[0].TX_ID = (framecontrol[0].start)[4];
        tx_id = framecontrol[0].TX_ID;

        framecontrol[0].lOCK_STATE = 1;
        //framecontrol[0].RX_ID = (framecontrol[0].start)[6];
        framecontrol[0].MREADY == (framecontrol[0].start)[15];

        (framecontrol[0].start)[3] = framecontrol[0].lOCK_STATE;
        //qDebug()<<"lock-state done";
    }
    else{
        //qDebug()<<"lock-state pass";
    }

    if(rx_temp != framecontrol[0].RX_ID){

        rx_temp = framecontrol[0].RX_ID;
        end_time0 = QTime::currentTime();

        int tt;
        tt = start_time0.msecsTo(end_time0);
        qDebug() << "chip compute time: "<< tt;
        start_time0 = QTime::currentTime();
    }


    tx_id = loop_id(tx_id);
    if(framecontrol[0].TX_ID == tx_id){
        tx_id = loop_id(tx_id);
    }

    //qDebug() << "TX_ID"<< framecontrol[0].TX_ID;


    vdma_start_buffering(&vdma2, fbaddr2, tx_id, VDMA_READ);
    vdma_start_buffering(&vdma1, fbaddr1, (int)buf.index, VDMA_READ);

    //qDebug()<<"vdma_start_buffering";

    while(1!=vdma_running(&vdma1, 1)){qDebug() << "vdma1_IsNotReady";usleep(100);}


    XCvt_resize_Start(&Cvt_resize);

    //qDebug()<<"XCvt_resize_Start";


    vdma_start_buffering(&vdma0, fbaddr0, (int)buf.index, VDMA_WRITE);




    while(0!=XCvt_resize_IsReady(&Cvt_resize)){qDebug() << "XCvt_resize_IsNotReady";usleep(100);}


    while(0==XCvt_resize_IsDone(&Cvt_resize))
    {
        usleep(100);
        //qDebug()<<"wait XCvt_resize_IsDone";
    }



    while(0!=vdma_running(&vdma1, 1)){
        //qDebug() << "vdma1_IsNotDone";
        usleep(100);
    }

    cvSetData(IM_show[shownum],fbaddr1[buf.index].fbV,960*4);
    if(RTSP){

    }
    else if(debug){
        memcpy(fbaddr2[tx_id].fbV, in_802816,802816*sizeof(short));
        msync((fbaddr2[tx_id].fbV), 802816*sizeof(short), MS_SYNC);
    }/*else{
        qDebug() << "start write img to dat";
        ofstream outFile("input_img.dat", ios::out | ios::binary);
        outFile.write((char*)fbaddr2[tx_id].fbV, 802816*sizeof(short));
        outFile.close();
        qDebug() << "end write img to dat";
    }*/
    // Re-queen buffer

    if(USE_CAMERA)ioctl(fd, VIDIOC_QBUF, &buf);

}

Thread_O::Thread_O()
{

}

void Thread_O::run()
{

    msync((framecontrol[0].start), 24*sizeof(int), MS_SYNC);


        //debug mode

        //readstate=1;
        //(framecontrol[0].start)[0] = 1;
        //

        //framecontrol[0].RS = (framecontrol[0].start)[0];

        if(readstate_open==0){


            //thread.start();

            readstate_open=1;


            //qDebug()<<"transfer complete";

            unsigned int temp;
            int k = 0;
            const char* ch = "/home/linaro/conv9.txt";
            /*
            ifstream file_name(ch);
            if (!file_name.is_open())
            {
                qDebug() << "open file error!";
            }


            while (!file_name.eof())
            {
                file_name>>hex>>temp;
                back_25088[k] = (short)temp;

                if(k<2){
                    qDebug()<<temp;
                }
                k++;

            }
            qDebug()<<k;
            file_name.close();
            */

            ch = "/home/linaro/conv0.txt";

            ifstream file_name2(ch);
            if (!file_name2.is_open())
            {
                qDebug() << "open file error!";
            }


            k = 0;

            while (!file_name2.eof())
            {
                file_name2>>hex>>temp;
                in_802816[k] = (short)temp;

                if(k<2){
                    qDebug()<<temp;
                }
                k++;

            }
            qDebug()<<k;
            file_name2.close();

        }


    int shotp = loop_id(shownum_temp);
    //qDebug()<<"getdata: "<< shotp;
    camera_get(shotp);
    //camera_get(shownum_temp);
    shownum_temp = shotp;
    end_time = QTime::currentTime();

    int tt;
    tt = start_time.msecsTo(end_time);

    //int tt2;
    //tt2 = start_time_t.msecsTo(end_time);

    start_time = QTime::currentTime();

    qDebug() << "display time: " << tt;
    //qDebug() << "display time2: " << tt2;

    //qDebug() << "start_time:" << start_time_t.msec();
    //qDebug() << "end_time:" << end_time.msec();

}
